/**
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.hadoop.hive.ql.exec.mr3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public class MR3Log {

	private String dag;

	public MR3Log(String dag) {
		this.dag = dag;
	}

	public static class LogMapper extends MapReduceBase implements
			Mapper<LongWritable, Text, Text, IntWritable> {

		@Override
		public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output,
				Reporter reporter) throws IOException {
			StringBuilder dag = new StringBuilder();
			Path pt = new Path("/tmp/test.dot");
			FileSystem fs = FileSystem.get(new Configuration());
			BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(pt)));
			String line;
			line = br.readLine();
			while (line != null) {
				dag.append(line);
				dag.append(System.getProperty("line.separator"));
				line = br.readLine();
			}
			br.close();
			System.err.println(dag.toString());

			// output.collect(new Text("mapout"), new IntWritable(100));
		}
	}

	public static class LogReducer extends MapReduceBase implements
			Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		public void reduce(Text key, Iterator<IntWritable> values,
				OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
			// output.collect(key, new IntWritable(200));
		}

	}

	public void run() throws IOException {
		String inDir = "/tmp/test.dot";
		String outDir = "/tmp/out";

		JobConf conf = new JobConf(MR3Log.class);
		conf.setJobName("Write log");

		FileInputFormat.addInputPath(conf, new Path(inDir));
		FileOutputFormat.setOutputPath(conf, new Path(outDir));

		conf.setMapperClass(LogMapper.class);
		conf.setReducerClass(LogReducer.class);

		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(IntWritable.class);

		// write dag to hdfs
		FileSystem fs = FileSystem.get(URI.create("/tmp"), conf);
		if (fs.exists(new Path(outDir))) {
			fs.delete(new Path(outDir), true);
		}
		Path file = new Path("/tmp/test.dot");
		if (fs.exists(file)) {
			fs.delete(file, true);
		}
		OutputStream os = fs.create(file);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
		bw.write(dag);
		bw.close();

		// read again for testing
		// StringBuilder dag = new StringBuilder();
		// Path pt = new Path("/tmp/test.dot");
		// BufferedReader br = new BufferedReader(new
		// InputStreamReader(fs.open(pt)));
		// String line;
		// line = br.readLine();
		// while (line != null) {
		// dag.append(line);
		// dag.append(System.getProperty("line.separator"));
		// line = br.readLine();
		// }
		// br.close();
		// System.err.println(dag.toString());

		JobClient.runJob(conf);
	}
}
